﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NetworkIndexer.Data.DomainModels;

namespace NetworkIndexer.Data.Repositories.Concrete
{
    public class DataAccess : IDataAccess
    {
        /// <summary>
        /// Adds a share to the cache (ex. to a database)
        /// </summary>
        /// <param name="details">The details of the share</param>
        public void AddShareToCache(ShareDetails details)
        {
            try
            {
                using (LocalStorageEntities localStorageContext = new LocalStorageEntities())
                {
                    localStorageContext.AddToNetworkShareCaches(new NetworkShareCache
                    {
                        share_id = details.ShareId,
                        computer_ip = details.ComputerIP,
                        share = details.SharePath,
                        accessible = details.IsShareAccessible,

                    });

                    localStorageContext.SaveChanges();
                }
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// Gets the network share cache
        /// </summary>
        public IList<ShareDetails> GetNetworkShareCache()
        {
            using (LocalStorageEntities localStorageContext = new LocalStorageEntities())
            {
                return localStorageContext.NetworkShareCaches.ToList().Select(x => new ShareDetails()
                {
                    ShareId = x.share_id,
                    ComputerIP = x.computer_ip,
                    SharePath = x.share,
                    IsShareAccessible = x.accessible
                }).ToList();
            }

        }

        /// <summary>
        /// Clears the network share cache
        /// </summary>
        public void ClearCache()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Recalculates the accessibility of the network paths without reloading the cache
        /// </summary>
        public void ReCalculateAccessibility()
        {
            throw new NotImplementedException();
        }
    }
}
